# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import random
import time
from cookies import lasthopes, randomword
import re
import string
import sys
from selenium.webdriver.common.keys import Keys
from PIL import Image
from os import remove, listdir, sep
from os.path import basename, isfile
import tempfile
from warnings import warn
from baseclass import BaseClass

class auth_logout(BaseClass):
    def test_auth_logout(self):
        driver = self.driver
        #Link to auth
        inputElement = driver.find_element_by_css_selector("div.b-header-top-auth")\
                             .find_element_by_css_selector("span.g-pseudo-h")
        inputElement.click()
        #Auth popup
        auth_pop = self.wait_for_popub_name(u'Авторизация')
        #Auth button
        auth_btn = WebDriverWait(auth_pop, timeout=10)\
                   .until(lambda dr: dr.find_element_by_css_selector("input.b-sbutton-input"))
        #Credentials
        inp_mail = auth_pop.find_element_by_name('email')
        inp_pass = auth_pop.find_element_by_name('password')
        inp_mail.send_keys("sotmail.sotm@yandex.ru")
        inp_pass.send_keys('132785')
        auth_btn.click()
        url_before = driver.current_url
        #Wait for auth popup dissapeared
        WebDriverWait(driver, timeout=10)\
                   .until(lambda dr: not self.check_exists(auth_pop))
        #Wait for auth link dissapeared
        WebDriverWait(driver, timeout=10)\
                   .until(lambda dr: not self.check_exists(inputElement))
        #Wait for link to cabinet
        a = WebDriverWait(driver, timeout=10)\
                   .until(lambda dr: dr.find_element_by_css_selector("a.b-header-top-auth-profile-anchor.js-construct"))
        assert u'Личный кабинет' in a.text
        a.click()
        #Logout
        out_btn = WebDriverWait(driver, timeout=10)\
                               .until(lambda dr: dr.find_element_by_css_selector("span.b-sbutton.js-logout")\
                                                   .find_element_by_css_selector("input.b-sbutton-input"))
        out_btn.click()
        #Wait for auth button appeared again
        WebDriverWait(driver, timeout=10)\
                               .until(lambda dr: dr.find_element_by_css_selector("div.b-header-top-auth")\
                                                   .find_element_by_css_selector("span.g-pseudo-h"))
